﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BT_web__nhom06.BiThuChiDoan
{
    public partial class DSHoatDongDoan : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                DataTable tbl2 = Tienichsql.ketnoicsdl.ChayLenhSelectDuLieu("SELECT *  FROM HoatDongDoan ");
                gvQLHoatDongDoan.DataSource = tbl2;
                gvQLHoatDongDoan.DataBind();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string pSQL = "Select  * from hoatdongdoan";
            DataTable pBang;
            pBang = Tienichsql.ketnoicsdl.ChayLenhSelectDuLieu(pSQL);

            string name_ = "DSHoatDongDoan"; //Tên file excel mà bạn lưu về máy


            //Tạo mới bảng để chép vào file excel
            Table tb = new Table();
            //Định dạng bảng
            tb.BorderColor = System.Drawing.Color.FromName("red");
            tb.CellPadding = 4;
            tb.GridLines = GridLines.Both;
            tb.CellSpacing = 0;
            tb.Width = Unit.Percentage(100);
            TableCell cell;
            TableRow row;
            int from = 0;
            int to = pBang.Rows.Count;
            int header = 0;
            for (int i = from; i < to; i++)
            {
                DataRow dr = pBang.Rows[i];
                if (header == 0)
                {
                    row = new TableRow();
                    cell = new TableCell();
                    //Ma sinh vien
                    cell.Height = 50;
                    cell.BackColor = System.Drawing.Color.FromName("red");
                    cell.Width = 150;
                    cell.Text = "<b>Mã Hoạt Động</b>";
                    cell.HorizontalAlign = HorizontalAlign.Center;
                    row.Cells.Add(cell);
                    //Tên hoạt động
                    cell = new TableCell();
                    cell.Height = 50;
                    cell.Width = 150;
                    cell.BackColor = System.Drawing.Color.FromName("red");
                    cell.Text = "<b>Tên Hoạt Động</b>";
                    cell.HorizontalAlign = HorizontalAlign.Center;
                    row.Cells.Add(cell);
                    //Mô tả
                    cell = new TableCell();
                    cell.Height = 50;
                    cell.BackColor = System.Drawing.Color.FromName("red");
                    cell.Width = 150;
                    cell.Text = "<b>Mô Tả</b>";
                    cell.HorizontalAlign = HorizontalAlign.Center;
                    row.Cells.Add(cell);
                    //Thời gian bắt đầu
                    cell = new TableCell();
                    cell.Height = 50;
                    cell.BackColor = System.Drawing.Color.FromName("red");
                    cell.Width = 150;
                    cell.Text = "<b>Thời Gian Bắt Đầu</b>";
                    cell.HorizontalAlign = HorizontalAlign.Center;
                    row.Cells.Add(cell);
                    //Thời gian kết thúc
                    cell = new TableCell();
                    cell.Height = 50;
                    cell.BackColor = System.Drawing.Color.FromName("red");
                    cell.Width = 150;
                    cell.Text = "<b>Thời Gian Kết Thúc</b>";
                    cell.HorizontalAlign = HorizontalAlign.Center;
                    row.Cells.Add(cell);
                    //Địa Điểm
                    cell = new TableCell();
                    cell.Height = 50;
                    cell.BackColor = System.Drawing.Color.FromName("red");
                    cell.Width = 150;
                    cell.Text = "<b>Địa Điểm</b>";
                    cell.HorizontalAlign = HorizontalAlign.Center;
                    row.Cells.Add(cell);
                    //Họ tên người phụ trách
                    cell = new TableCell();
                    cell.Height = 50;
                    cell.BackColor = System.Drawing.Color.FromName("red");
                    cell.Width = 150;
                    cell.Text = "<b>Họ Tên Người Phụ Trách</b>";
                    cell.HorizontalAlign = HorizontalAlign.Center;
                    row.Cells.Add(cell);
                    //Số điện thọai người phụ trách
                    cell = new TableCell();
                    cell.Height = 50;
                    cell.BackColor = System.Drawing.Color.FromName("red");
                    cell.Width = 150;
                    cell.Text = "<b>Số Điện Thoại Người Phụ Trách</b>";
                    cell.HorizontalAlign = HorizontalAlign.Center;
                    row.Cells.Add(cell);
                    //Tình trạng
                    cell = new TableCell();
                    cell.Height = 50;
                    cell.BackColor = System.Drawing.Color.FromName("red");
                    cell.Width = 150;
                    cell.Text = "<b>Tình Trạng</b>";
                    cell.HorizontalAlign = HorizontalAlign.Center;
                    row.Cells.Add(cell);

                    tb.Rows.Add(row);

                }

                header++;
                row = new TableRow();
                cell = new TableCell();

                //mã hoạt động
                cell.Height = 50;
                cell.Text = dr[0].ToString();
                cell.HorizontalAlign = HorizontalAlign.Left;
                row.Cells.Add(cell);

                //Tên hoạt động
                cell = new TableCell();
                cell.Height = 50;
                cell.Text = dr[1].ToString();
                cell.HorizontalAlign = HorizontalAlign.Left;
                row.Cells.Add(cell);

                //Mô tả

                cell = new TableCell();
                cell.Height = 50;
                cell.Text = dr[2].ToString().Trim();
                cell.HorizontalAlign = HorizontalAlign.Left;
                row.Cells.Add(cell);
                //tg bat dau
                cell = new TableCell();
                cell.Height = 50;
                cell.Text = dr[3].ToString().Trim();
                cell.HorizontalAlign = HorizontalAlign.Left;
                row.Cells.Add(cell);
                //tg ket thuc
                cell = new TableCell();
                cell.Height = 50;
                cell.Text = dr[4].ToString().Trim();
                cell.HorizontalAlign = HorizontalAlign.Left;
                row.Cells.Add(cell);
                //dia diem
                cell = new TableCell();
                cell.Height = 50;
                cell.Text = dr[5].ToString();
                cell.HorizontalAlign = HorizontalAlign.Left;
                row.Cells.Add(cell);
                //ho ten nguoi phu trach
                cell = new TableCell();
                cell.Height = 50;
                cell.Text = dr[6].ToString().Trim();
                cell.HorizontalAlign = HorizontalAlign.Left;
                row.Cells.Add(cell);
                //so dien thoai nguoi phu trach
                cell = new TableCell();
                cell.Height = 50;
                cell.Text = dr[7].ToString().Trim();
                cell.HorizontalAlign = HorizontalAlign.Left;
                row.Cells.Add(cell);

                //Tinh trang
                cell = new TableCell();
                cell.Height = 50;
                if ((string)dr[8] == "1")
                { cell.Text = "Đang hoàn thành"; }
                else
                { cell.Text = "Đã hoàn thành"; }
                cell.HorizontalAlign = HorizontalAlign.Left;
                row.Cells.Add(cell);
                tb.Rows.Add(row);

            }

            //- Tương tự bạn có bao nhiêu trường cần export thì bạn chèn bấy nhiêu cột.

            //

            Response.Clear();

            Response.Buffer = true;

            //excel

            string ex_ = "xls";

            Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + name_ + "." + ex_);

            //    Context.Response.AddHeader("Content-Length", strpath.Length.ToString());

            Response.ContentType = "application/vnd.ms-excel";

            Response.Charset = "UTF-8";

            this.EnableViewState = false;

            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();

            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

            tb.RenderControl(oHtmlTextWriter);

            Response.Write(oStringWriter.ToString());
            Response.End();

        }
    }
}